package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.repository.CourseElasticsearchRepository;
import cn.itsource.hrm.service.ISearchService;
import cn.itsource.hrm.util.PageList;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

@Service
public class SearchServiceImpl implements ISearchService {
    @Autowired
    private CourseElasticsearchRepository repository;
    @Override
    public void save(CourseDoc doc) {
        repository.save(doc);
    }

    @Override
    public void delete(CourseDoc doc) {
        repository.deleteById(doc.getId());
    }

    /**
     * 根据条件查询列表
     * @param courseQuery
     * @return
     */
    @Override
    public PageList searchCourse(CourseQuery courseQuery) {
        //1.构建查询条件对象
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //2.设置排序字段
        if(StringUtils.isNotEmpty(courseQuery.getSortField())){
            //默认排序按上线时间
            String sortFiled = "onlineTime";
            switch (courseQuery.getSortField().toLowerCase()){
                //销量
                case "xl": sortFiled = "saleCount"; break;
                //价格
                case "jg": sortFiled="price";break;
                //评论数
                case"pl":sortFiled="commentCount";break;
                //人气
                case"rq":sortFiled="viewCount";break;
            }
            //3.排序方式(默认升序)
            SortOrder desc = SortOrder.DESC;
            if(StringUtils.isNotEmpty(courseQuery.getSortType())&&
                    courseQuery.getSortType().equalsIgnoreCase("asc")) {
                    desc=SortOrder.ASC;
            }
            builder.withSort(SortBuilders.fieldSort(sortFiled).order(desc));
        }
        //4.分页
        builder.withPageable(PageRequest.of(courseQuery.getPage()-1, courseQuery.getRows()));
        //5.构建查询条件对象
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //课程名
        if(StringUtils.isNotEmpty(courseQuery.getKeyword())){
            boolQueryBuilder.must(QueryBuilders.matchQuery("name", courseQuery.getKeyword()));
        }
        //课程分类
        if(null != courseQuery.getCourseTypeId()){
            boolQueryBuilder.filter(QueryBuilders.termQuery("courseTypeId",courseQuery.getCourseTypeId()));
        }
        //课程等级
        if(StringUtils.isNotEmpty(courseQuery.getGradeName())){
            boolQueryBuilder.filter(QueryBuilders.termQuery("gradeName", courseQuery.getGradeName()));
        }
        //机构
        if(StringUtils.isNotEmpty(courseQuery.getTenantName())){
            boolQueryBuilder.filter(QueryBuilders.termQuery("tenantName", courseQuery.getTenantName()));
        }
        //最大价格
        if(null!=courseQuery.getPriceMax()){
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").lte(courseQuery.getPriceMax()));
        }
        //最小价格
        if(null!=courseQuery.getPriceMin()){
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(courseQuery.getPriceMin()));
        }
        //放入查询条件
        builder.withQuery(boolQueryBuilder);
        //进行查询
        Page<CourseDoc> page= repository.search(builder.build());
        //转化为PageList
        PageList courseDocPageList = new PageList<>(page.getTotalElements(), page.getContent());
        return courseDocPageList;
    }
}
